// import Vue from 'vue'
import toast from "@/components/wms/Toast.vue";
import Vue from "vue";
import Vuetify from 'vuetify/lib';
const ToastConstructor = Vue.extend(toast)

const instances = [];

let defaultOptions = {
    x: 'right', // default
    y: 'bottom', // default
    color: 'info', // default
    icon: 'info',
    timeout: 3000, // default
    dismissable: true, // default
    autoHeight: false, // default
    multiLine: false, // default
    vertical: false, // default
    shorts: {
        custom: {
            color: 'purple'
        }
    },
    property: '$toast' // default
}

const isServer = typeof window === 'undefined';
let cmp = null
const Toast = function (options) {
    if (isServer) return;
    const component = ToastConstructor()
    const vuetifyObj = new Vuetify()
    component.$vuetify = vuetifyObj.framework
    const componentOptions = { ...defaultOptions, ...options }

    if (componentOptions.slot) {
        component.$slots.default = componentOptions.slot
        delete componentOptions.slot
    }

    Object.assign(component, componentOptions)
    document.body.appendChild(component.$el)

    instances.push(component);
    return component
}

Toast.show = function (message, options = {}) {
    if (cmp) {
        const isQueueable = options.queueable !== undefined ? options.queueable : defaultOptions.queueable

        if (isQueueable) {
            instances.push({ message, options })
        } else {
            cmp.close()
            instances.unshift({ message, options })
        }

        return
    }

    options.message = message
    cmp = Toast(options)
    cmp.$on('statusChange', (isActive, wasActive) => {
        if (wasActive && !isActive) {
            cmp.$nextTick(() => {
                cmp.$destroy()
                cmp = null

                if (instances.length) {
                    const toast = instances.shift()
                    Toast.show(toast.message, toast.options)
                }
            })
        }
    })
}

Toast.shorts = function (message, options, color) {
    const methods = {}

    methods[color] = (message, options) => Toast.show(message, { color, ...options })

    if (options.shorts) {
        for (let key in options.shorts) {
            const localOptions = options.shorts[key]
            methods[key] = (message, options) => Toast.show(message, { ...localOptions, ...options })
        }
    }

    return methods
}

let list = ['success', 'info', 'error', 'warning']

list.forEach((mode) => {
    Toast[mode] = function (content, options, mode) {
        Toast.shorts(content, options, mode)
    };
});

Toast.config = function (options) {
    if (!options || Array.isArray(options) || typeof options !== 'object') return defaultOptions;
    for (const key in options) {
        // eslint-disable-next-line no-prototype-builtins
        if (!options.hasOwnProperty(key)) continue;
        defaultOptions[key] = options[key];
    }
    return defaultOptions;
};


Toast.getCmp = () => {
    return cmp
}

Toast.clearQueue = () => {
    return instances.splice(0, instances.length)
}



































// function init(Vue, globalOptions = defaultOptions) {
//     console.log(12121)

//     let cmp = null
//     const queue = []
//     const property = globalOptions.property || '$toast'

//     function createCmp(options) {
//         console.log(12121)
//         const component = ToastConstructor()
//         const vuetifyObj = new Vuetify()
//         component.$vuetify = vuetifyObj.framework
//         const componentOptions = { ...Vue.prototype[property].globalOptions, ...options }

//         if (componentOptions.slot) {
//             component.$slots.default = componentOptions.slot
//             delete componentOptions.slot
//         }

//         Object.assign(component, componentOptions)
//         document.body.appendChild(component.$el)

//         return component
//     }

//     function show(message, options = {}) {
//         if (cmp) {
//             const isQueueable = options.queueable !== undefined ? options.queueable : globalOptions.queueable

//             if (isQueueable) {
//                 queue.push({ message, options })
//             } else {
//                 cmp.close()
//                 queue.unshift({ message, options })
//             }

//             return
//         }

//         options.message = message
//         cmp = createCmp(options)
//         cmp.$on('statusChange', (isActive, wasActive) => {
//             if (wasActive && !isActive) {
//                 cmp.$nextTick(() => {
//                     cmp.$destroy()
//                     cmp = null

//                     if (queue.length) {
//                         const toast = queue.shift()
//                         show(toast.message, toast.options)
//                     }
//                 })
//             }
//         })
//     }

//     function shorts(options) {
//         const colors = ['success', 'info', 'error', 'warning']
//         const methods = {}

//         colors.forEach(color => {
//             methods[color] = (message, options) => show(message, { color, ...options })
//         })

//         if (options.shorts) {
//             for (let key in options.shorts) {
//                 const localOptions = options.shorts[key]
//                 methods[key] = (message, options) => show(message, { ...localOptions, ...options })
//             }
//         }

//         return methods
//     }

//     function getCmp() {
//         return cmp
//     }

//     function clearQueue() {
//         return queue.splice(0, queue.length)
//     }

//     Vue.prototype.$toast = Object.assign(show, {
//         globalOptions,
//         getCmp,
//         clearQueue,
//         ...shorts(globalOptions)
//     })
// }

// if (typeof window !== 'undefined' && window.Vue) {
//     window.Vue.use(init)
// }

Toast.install = function (Vue, options) {
    Toast.config(options);
    Vue.prototype.$toast = Toast;
};

export default Toast
